<?php 


namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\admin\controller\Admin;
use think\Paginator;

/**
* 
*/
class manpower extends Admin
{
    /**
     * [index 加载首页]
     */
    public function index()
    {
        $manpower = db('_employee_information')
                    ->where('status='.'1')
                    ->whereOr('status='.'0')
                    ->order('id')
                    ->paginate(5);
                    //var_dump($manpower);
        //echo db('_employee_information')->getlastsql();die;
        //var_dump($manpower);die;
        return view('admin@manpower/index',[
            'manpower' => $manpower
        ]);
    }


    /**
     * [addman 加载添加人力页面]
     */
    public function addman()
    {
        return view('admin@manpower/addman');
    }


    /**
     * [doaddman 加载添加人力页面]
     */
    public function doaddman()
    {
        $res = input('param.');
        //var_dump($res);
        if ($res['status'] == 1) {
            $newtime = time();
            $time = date("Y-m-d",$newtime);

            if ($res['incumbent_position'] == '') {
                return $this->error('现任职位不能为空');
            }
            if ($res['entry_time'] == '') {
                return $this->error('入职日期不能为空');
            }
            /*if ($res['out_time'] != '') {
                if ($res['out_time']< $res['entry_time']) {
                    return $this->error('离职日期不能小于入职日期');
                }
            }$res['out_time'] = null;*/
            //var_dump($res);die;
            if (preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u',$res['incumbent_position'])) {
                if ($res['entry_time']>$time) {
                    return $this->error('入职日期不合规范');
                }
                //var_dump($res);die;
                $result = db('_employee_information')
                  ->insert($res);
                if ($result > 0) {
                    return $this->success('添加成功!', url('admin/manpower/index'));
                } else {
                    return $this->error('添加失败!');
                }
            }
            return $this->error('现任职位不合规范');
        } else{
            //var_dump($res);
            $res['incumbent_position'] = null;
            $res['entry_time'] = null;
            //$res['out_time'] = null;
            //var_dump($res);die;
            $result = db('_employee_information')
                      ->insert($res);

            if ($result > 0) {
                return $this->success('添加成功!', url('admin/manpower/index'));
            } else {
                return $this->error('添加失败!');
            }
        }
        
        
    }
    

    /**
     * [editman 加载编辑人力页面]
     */
    public function editman()
    {
        $p = input('param.');
        //var_dump($p);die;
        $id = $p['id'];
        $res = db('_employee_information')->field(['id','name','birthday','graduate_institutions','education_background','major','tel','incumbent_position','entry_time','out_time','status'])->find($id);
        //var_dump($res);die;
        return view('admin@manpower/editman',[
            'res' => $res
        ]);
    }

    /**
     * [doeditman 执行安全计划编辑]
     */
    public function doeditman()
    {
        $res = input('param.');
        //var_dump($res);die;
        $id = $res['id'];
        if ($res['status'] == 1) {
            $newtime = time();
            $time = date("Y-m-d",$newtime);

            if ($res['incumbent_position'] == '') {
                return $this->error('现任职位不能为空');
            }
            if ($res['entry_time'] == '') {
                return $this->error('入职日期不能为空');
            }
            if ($res['out_time'] != '') {
                if ($res['out_time']< $res['entry_time']) {
                    return $this->error('离职日期不能小于入职日期');
                }
                $res['status'] = 2;
            }
            if ($res['out_time'] == '') {
                $res['out_time'] = null;
            }
            //var_dump($res);die;
            if (preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u',$res['incumbent_position'])) {
                if ($res['entry_time']>$time) {
                    return $this->error('入职日期不合规范');
                }
                //var_dump($res);die;
                $result = db('_employee_information')->where('id='.$id)->update($res);
                //var_dump($res);die;
                if ($result > 0) {
                    return $this->success('编辑成功!', url('admin/manpower/index'));
                } else {
                    return $this->error('你未修改任何内容');
                }
            }
            return $this->error('现任职位不合规范');
        } elseif($res['status'] == 0) {
            $res['incumbent_position'] = null;
            $res['entry_time'] = null;
            $res['out_time'] = null;
            $result = db('_employee_information')->where('id='.$id)->update($res);
            //var_dump($res);die;
            if ($result > 0) {
                return $this->success('编辑成功!', url('admin/manpower/index'));
            } else {
                return $this->error('你未修改任何内容');
            }
        }else{
            $newtime = time();
            $time = date("Y-m-d",$newtime);

            if ($res['incumbent_position'] == '') {
                return $this->error('现任职位不能为空');
            }
            if ($res['entry_time'] == '') {
                return $this->error('入职日期不能为空');
            }
            if ($res['out_time'] != '') {
                if ($res['out_time']< $res['entry_time']) {
                    return $this->error('离职日期不能小于入职日期');
                }
                $res['status'] = 2;
            }
            if ($res['out_time'] == '') {
                return $this->error('离职日期不能为空');
            }
            //var_dump($res);die;
            if (preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u',$res['incumbent_position'])) {
                if ($res['entry_time']>$time) {
                    return $this->error('入职日期不合规范');
                }
                //var_dump($res);die;
                $result = db('_employee_information')->where('id='.$id)->update($res);
                //var_dump($res);die;
                if ($result > 0) {
                    return $this->success('编辑成功!', url('admin/manpower/index'));
                } else {
                    return $this->error('你未修改任何内容');
                }
            }
            return $this->error('现任职位不合规范');
        }
        

        
    }

    /**
     * [dodelman 加载添加人力页面]
     */
    public function dodelman()
    {
        $p = input('param.');
        //var_dump($p);exit;
        //把用户角色表中相关的也删除
        if(db('_employee_information')->where('id',$p['id'])->delete()){
            return $this->success("删除成功！",url('admin/manpower/index'));
        }else{
            return $this->error("删除失败");
        }
    }



    /**
     * [dostatus 改变状态]
     */
    public function dostatus()
    {
        $p = input('param.');
        //var_dump($p);die;
        $id = $p['id'];

        $status = db('_employee_information')->field('status')->where('id='.$id)->find();
        //var_dump($status);die;
        $status['status'] = strval($status['status'] == 1?0:1);
        $status['entry_time'] = date('Y-m-d',time());
        //var_dump($status);die;
        $result = db('_employee_information')->where('id='.$id)->update($status);
        //var_dump($result);die;
        
        return $this->redirect('admin/manpower/index');
    }

    /**
     * [delindex 加载首页]
     */
    public function delindex()
    {
        $manpower = db('_employee_information')
                    ->where('status='.'2')
                    ->order('id')
                    ->paginate(5);
        //var_dump($manpower);die;
        return view('admin@manpower/delindex',[
            'manpower' => $manpower
        ]);
    }

    /**
     * [recovery 恢复]
     */
    public function recovery()
    {
        $res = input('param.');
        //var_dump($res);
        $find = db('_employee_information')->where('id='.$res['id'])->find();
        //var_dump($find);die;
        $a['status'] = 1;
        $a['out_time'] = null;

        $manpower = db('_employee_information')
                    ->where('id='.$res['id'])
                    ->update($a);
        //var_dump($manpower);die;
        return $this->redirect('admin/manpower/delindex');
    }


    /**
     * [del 放入回收站]
     */
    public function del()
    {
        $res = input('param.');
        //var_dump($res);
        $find = db('_employee_information')->where('id='.$res['id'])->find();
        //var_dump($find);die;
        $a['status'] = 2;
        $newtime = time();
        $time = date("Y-m-d",$newtime);
        if ($find['out_time']== '') {
            $a['out_time'] = $time;
        }
        $manpower = db('_employee_information')
                    ->where('id='.$res['id'])
                    ->update($a);
        //var_dump($manpower);die;
        return $this->redirect('admin/manpower/index');
    }














    /**
     * [bulletin 加载招聘公告表]
     */
    public function bulletin()
    {
        $manpower = db('_recruitment_bulletin')
            ->paginate(3);
        //var_dump($manpower);die;
        return view('admin@manpower/bulletin',[
            'manpower' => $manpower
        ]);
    }


    /**
     * [addbulletin 加载添加招聘公告页面]
     */
    public function addbulletin()
    {

        //var_dump($manpower);die;
        return view('admin@manpower/addbulletin');
    }


    /**
     * [doaddbulletin 执行添加招聘公告]
     */
    public function doaddbulletin()
    {
        $res = input('param.');

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        if (!$file) {
            $this->error('请先上传图片');
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){

            $info = $file->validate(['size'=>1024*1024*3,'ext'=>'jpg,png,gif'])->move(ROOT_PATH .'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                //echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                //echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                //echo $info->getFilename();
            } else {
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }

        //var_dump($info->getSaveName());die;
        
        //var_dump($res);die;
        // 20170929xxx.jpg  =>  ../Uploads/2017/09/29/20170929xxx.jpg
        

        //20181009\b503fa934c8a87ac4db86a5c08956d54.jpg
        $url = '/uploads/';
        $url .= substr($info->getSaveName(), 0, 8).'/';
        $url .= $info->getFilename();
        $res['image'] = $url;
        //var_dump($res);die;
        //var_dump($res);die;
        $res['recruitment_unit'] = htmlentities($res['recruitment_unit']);
        $res['recruitment_position'] = htmlentities($res['recruitment_position']);
        $res['recruitment_num'] = htmlentities($res['recruitment_num']);
        $res['job_description'] = htmlentities($res['job_description']);
        $res['orporate_welfare'] = htmlentities($res['orporate_welfare']);
        $res['recruitment_unit'] = htmlentities($res['salary']);
        $result = db('_recruitment_bulletin')
                  ->insert($res);

        if ($result > 0) {
            return $this->success('添加成功!', url('admin/manpower/bulletin'));
        } else {
            return $this->error('添加失败!');
        }

    }



    
    /**
     * [editbulletin 加载编辑招聘公告]
     */
    public function editbulletin()
    {
        $p = input('param.');
        //var_dump($p);die;
        $id = $p['id'];
        $res = db('_recruitment_bulletin')->field(['id','recruitment_unit','recruitment_position','recruitment_num','job_description','orporate_welfare','salary','image','status'])->find($id);
        //var_dump($res);die;
        return view('admin@manpower/editbulletin',[
            'res' => $res
        ]);

    }


     /**
     * [doeditbulletin 执行编辑招聘公告]
     */
    public function doeditbulletin()
    {
        $p = input('param.');
        
        //var_dump($p);die;
        $id = $p['id'];
        $res = db('_recruitment_bulletin')->field(['image','status'])->find($id);
        if (!$res['image']) {
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('image');
            if (!$file) {
                $this->error('请先上传图片');
            }
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){

                $info = $file->validate(['size'=>1024*1024*3,'ext'=>'jpg,png,gif'])->move(ROOT_PATH .'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 jpg
                    //echo $info->getExtension();
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    //echo $info->getSaveName();
                    // 输出 42a79759f284b767dfcb2a0197904287.jpg
                    //echo $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

            //var_dump($info->getSaveName());die;
            
            //var_dump($p);die;
            // 20170929xxx.jpg  =>  ../Uploads/2017/09/29/20170929xxx.jpg
            

            //20181009\b503fa934c8a87ac4db86a5c08956d54.jpg
            $url = '/uploads/';
            $url .= substr($info->getSaveName(), 0, 8).'/';
            $url .= $info->getFilename();
            $p['image'] = $url;
        }
        //var_dump($res);die;
        $res['recruitment_unit'] = htmlentities($res['recruitment_unit']);
        $res['recruitment_position'] = htmlentities($res['recruitment_position']);
        $res['recruitment_num'] = htmlentities($res['recruitment_num']);
        $res['job_description'] = htmlentities($res['job_description']);
        $res['orporate_welfare'] = htmlentities($res['orporate_welfare']);
        $res['recruitment_unit'] = htmlentities($res['salary']);

        $result = db('_recruitment_bulletin')->where('id='.$id)->update($p);
        //var_dump($result);die;
        if ($result > 0) {
            return $this->success('编辑成功!', url('admin/manpower/bulletin'));
        } else {
            return $this->error('你未修改任何内容');
        }

    }






    /**Ajax 删除
     * [delete 执行安全计划删除]
     */
    public function delete($id)
    {
        $result = db('_recruitment_bulletin')->delete($id);
        // var_dump($result); exit;
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的招聘公告删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为:'.$id.'的招聘公告删除失败!';
        }

        return json($info);
    }



    public function _empty(Request $request)
    {
        return view('/error');
    }




}

 ?>